﻿using System;

namespace DevOnServer
{
    public class CChatHandler
    {
        public CChatHandler(CGameServer server)
        {
            m_server = server;
        }

        private CGameServer m_server;

        public void chat(CGamePlayer player, string message)
        {
            if (message.Length == 0)
                return;

            if (message[0] == '/')
            {
                string[] values = message.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                bool cmd = true;

                switch (values[0])
                {
                    case "/teleport":
                        _cmd_Teleport(player, values);
                        break;
                    default:
                        cmd = false;
                        break;
                }

                if (cmd)
                    return;
            }

            player.Object.World.sendToLocalArea(player.Object, CPacketCreator.chat(player.Object.ID, message));
        }

        private void _cmd_Teleport(CGamePlayer player, string[] values)
        {
            if (player.Account.AccessLevel < (int)AccessLevel.GameMaster)
                return;

            if (values.Length < 4)
                return;

            int worldId = int.Parse(values[1]);
            float x = float.Parse(values[2]);
            float z = float.Parse(values[3]);

            if (m_server.Worlds.ContainsKey(worldId))
            {
                player.Object.teleport(new CVec3Df(x, 100.0f, z), m_server.Worlds[worldId]);
            }
            else
            {
                player.greenText("La map avec l'ID {0} n'existe pas.", worldId);
            }
        }
    }
}